package com.small.nacos.naming.consistency.ephemeral.distro.component;

import com.small.nacos.core.distributed.distro.component.DistroDataStorage;
import com.small.nacos.core.distributed.distro.entity.DistroData;
import com.small.nacos.core.distributed.distro.entity.DistroKey;
import com.small.nacos.naming.cluster.transport.Serializer;
import com.small.nacos.naming.consistency.Datum;
import com.small.nacos.naming.consistency.ephemeral.distro.DataStore;
import com.small.nacos.naming.core.DistroMapper;
import com.small.nacos.sys.utils.ApplicationUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhoujin
 * @Date 2022/11/13 18:25
 */
public class DistroDataStorageImpl implements DistroDataStorage {

    private final DataStore dataStore;

    private final DistroMapper distroMapper;

    public DistroDataStorageImpl(DataStore dataStore, DistroMapper distroMapper) {
        this.dataStore = dataStore;
        this.distroMapper = distroMapper;
    }

    @Override
    public DistroData getDistroData(DistroKey distroKey) {
        Map<String, Datum> result = new HashMap<>(1);
        Datum datum = dataStore.get(distroKey.getResourceKey());
        result.put(distroKey.getResourceKey(), datum);
        byte[] dataContent = ApplicationUtils.getBean(Serializer.class).serialize(result);
        return new DistroData(distroKey, dataContent);
    }
}
